package com.zjdiepu.www.biz.third;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.dao.third.JuheSeriesMapper;
import com.zjdiepu.www.ibiz.third.IJuheSeriesService;
import com.zjdiepu.www.model.third.dto.JuheSeries;
import com.zjdiepu.www.model.third.ext.JuheSeriesExt;

/**
 * 车系Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年03月23日 20时33分<br/>
 * @since 1.0<br/>
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED, timeout = 60)
public class JuheSeriesServiceImpl extends BaseService implements IJuheSeriesService {
	public static final Logger logger = LoggerFactory.getLogger(JuheSeriesServiceImpl.class);

	@Autowired
	private JuheSeriesMapper juheSeriesMapper;
	
	@Override
	public String selectNameBySeriesId(String seriesId) {
		JuheSeries juheSeries = new JuheSeries();
		juheSeries.setDpStatus(DpStatus.NORMAL.getCode());
		juheSeries.setXlid(seriesId);
		juheSeries = juheSeriesMapper.selectRefined(juheSeries);
		return null == juheSeries ? null : juheSeries.getXlname();
	}
	
	@Override
	public int selectSeriesId() {
		int maxSeriesId = juheSeriesMapper.selectMaxSeriesId();
		if(maxSeriesId > 1000000) {
			return maxSeriesId + 1;
		} else {
			return 1000001;
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void saveUpdateAndDelete(JuheSeries juheSeries, OperateType operType) {
		switch (operType) {
		case add:
			juheSeries.initInsertData(juheSeries.getModifier(),"张三");
			juheSeriesMapper.insert(juheSeries);
			break;
		case edit:
			juheSeries.initUpdateData(juheSeries.getModifier(),"张三");
			// 影响的记录数
			int updateCount = juheSeriesMapper.updateByPrimaryKeySelective(juheSeries);
			if(updateCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(updateCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		case delete:
			juheSeries.initDeleteData(juheSeries.getModifier(),"张三");
			// 影响的记录数
			int deleteCount = juheSeriesMapper.updateByPrimaryKeySelective(juheSeries);
			if(deleteCount == 0){
				throw new BaseException("CO_MSG_000012");
			} else if(deleteCount > 1){
				throw new BaseException("CO_MSG_000017");
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<JuheSeries> selectByExt(JuheSeriesExt juheSeriesExt) {
		return juheSeriesMapper.selectByExt(juheSeriesExt);
	}
	
	@Override
	public int countByExt(JuheSeriesExt juheSeriesExt){
		return juheSeriesMapper.countByExt(juheSeriesExt);
	}
	
	@Override
	public JuheSeries selectById(String id) {
		return juheSeriesMapper.selectByPrimaryKey(id);
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, timeout = 60)
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			juheSeriesMapper.clearEmpty(fieldNames, id);
		}
	}
	@Override
	public int countRefined(JuheSeries juheSeries) {
		return juheSeriesMapper.countRefined(juheSeries);
	}

	@Override
	public JuheSeries selectRefined(JuheSeries juheSeries) {
		return juheSeriesMapper.selectRefined(juheSeries);
	}
	

	@Override
	public boolean repeat(JuheSeries juheSeries) {
		int count = countRefined(juheSeries);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(juheSeries.getId())) {
			return true;
		}
		JuheSeries dbItem = selectRefined(juheSeries);
		if(null == dbItem) {
			return false;
		}
		return !juheSeries.getId().equals(dbItem.getId());
	}
}
